/***
 * 
 *  测试使用
 */
// import Home from '../pages/home'
import Login from '../pages/login'
import { lazyLoad, load, redirect, setRouteBefore } from './utilsFn/fn'
export const route = [
  {
    path: '/',
    element: redirect('/home')
  },
  {
    path: '/home',
    element: lazyLoad(() => import('../pages/home'), { title: '首页', needLogin: true }),// 懒加载
    children: [
      {
        path: 'option1',
        element: lazyLoad(() => import('../pages/option/options1'), { title: 'options11', needLogin: true }),// 懒加载
      },
      {
        path: 'option2',
        // element: lazyLoad(() => import('../pages/option/options2'), { title: 'options12', needLogin: true }),// 懒加载
        children: [
          {
            path: 'option21',
            element: lazyLoad(() => import('../pages/option/options2'), { title: 'options12', needLogin: true }),// 懒加载
          },
        ]
      },
      {
        path: 'option3',
        // element: lazyLoad(() => import('../pages/option/options5'), { title: 'options15', needLogin: true }),// 懒加载
        children: [
          {
            path: 'option31',
            element: lazyLoad(() => import('../pages/option/options6'), { title: 'options16', needLogin: true }),// 懒加载
          },
          {
            path: 'option32',
            element: lazyLoad(() => import('../pages/option/options7'), { title: 'options17', needLogin: true }),// 懒加载
          },
          {
            path: 'option33',
            element: lazyLoad(() => import('../pages/option/options8'), { title: 'options18', needLogin: true }),// 懒加载
          }
        ]
      },

    ]
  },
  {
    path: '/login',
    element: load(<Login />, { title: '登录', needLogin: false })// 普通加载
  },
  {
    path: '*',
    element: lazyLoad(() => import('../components/notFound'), { title: '页面丢失', needLogin: true })
  }
]

/**
 * @description: 全局路由拦截
 * @param {string} pathname 当前路由路径
 * @param {object} meta 当前路由自定义数据
 * @return {string} 需要跳转到其他页时返回该页的path路径
 */
const onRouteBefore = ({ pathname, meta }) => {
  // 动态修改页面title
  if (meta.title !== undefined) {
    document.title = meta.title
  }
  // 根据token判断是否登陆
  const isLogin = window.sessionStorage.getItem("token") ? true : false
  console.log(isLogin);
  // 判断未登录跳转登录页
  if (meta.needLogin) {
    if (!isLogin) {
      return '/login'
    }
  } else {
    // 清除登录的token
    window.sessionStorage.clear()
  }
}
setRouteBefore(onRouteBefore)